<?php namespace App\Http\Services;

use App\Userinfo;
use Auth;
use Blocktrail\CryptoJSAES\CryptoJSAES;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Cookie;
use App\User;

class UserManager
{
    public static function isLogin(Request $request = null,$front = 0)
    {
        if (!isset($_COOKIE['user_token'])) {
            if (is_null($request)) {
                return false;
            } else {
                $token = !is_null($request->header('user_token')) ? $request->header('user_token') : $request->get('user_token');
                if (!is_null($token)) {

                    $openid = false;
                    try {
                        $openid = CryptoJSAES::decrypt(str_replace(" ", "+", $token), env("AES_KEY"));
                        $openid = str_replace('wx_openid@' , "" , $openid);
                    } catch (\Exception $e) {
                        return false;
                    }

                    if (!$openid)
                        return false;

                    $thatUser = User::whereWxOpenid($openid)->first();
                    if (!$thatUser){
                        $thatUser = User::whereUserKey($openid)->first();
                        if(!$thatUser){
                            return false;
                        }
                    }

                    return $thatUser;
                } else {
                    return false;
                }
            }
        } else {
            $token = Cookie::get('user_token');

            //$front等于0，就会一直使用前端传来的user_token,$front不等于0会优先使用cookie中的user_token，若cookie中没有，才会使用前端传来的user_token
            if (!$token || is_null($token) || $token == '' || $front == 0){
                $token = !is_null($request->header('user_token')) ? $request->header('user_token') : $request->get('user_token');
            }

            if(!is_null($token)){
                $openid = false;
                try {
                    $openid = CryptoJSAES::decrypt(str_replace(" ", "+", $token), env("AES_KEY"));
                    $openid = str_replace('wx_openid@' , "" , $openid);
                } catch (\Exception $e) {
                    return false;
                }

                if (!$openid)
                    return false;

                $thatUser = User::whereWxOpenid($openid)->first();
                if (!$thatUser){
                    $thatUser = User::whereUserKey($openid)->first();
                    if(!$thatUser){
                        return false;
                    }
                }

                return $thatUser;
            }else{
                return false;
            }
        }
    }



    public static function buildRzKey()
    {
        $num = mt_rand(1000, 9999);
        $rzKey = md5(time() . $num);
        return $rzKey;
    }

    /**
     * 检查用户名是否已经存在或被使用
     * @param $username
     * @return bool
     */
    public static function isUsernameExists($username){
        if(User::whereUsername($username)->exists())
            return true;
        return false;
    }


}